﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace Desktop.Library
{
    [Serializable()]
    public class UserPreferences : ISerializable, INotifyPropertyChanged
    {
        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Variables

        private string _firstName;
        private string _lastName;
        private DateTime _birthDate;
        private GooglePreferences _googlePreferences;

        #endregion

        #region Constructors

        public UserPreferences()
        {
            FirstName = string.Empty;
            LastName = string.Empty;
            BirthDate = DateTime.Now;
            GooglePreferences = new GooglePreferences();
        }

        public UserPreferences(SerializationInfo info, StreamingContext context)
        {
            FirstName = info.GetString("FirstName");
            LastName = info.GetString("LastName");
            BirthDate = info.GetDateTime("BirthDate");
            GooglePreferences = info.GetValue("GooglePreferences", typeof(GooglePreferences)) as GooglePreferences;
        }

        #endregion

        #region Properties

        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                if (_firstName != value)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                    }
                }
                _firstName = value;
            }
        }

        public string LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                if (_lastName != value)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
                    }
                }
                _lastName = value;
            }
        }

        public DateTime BirthDate
        {
            get
            {
                return _birthDate;
            }
            set
            {
                if (_birthDate != value)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("BirthDate"));
                    }
                }
                _birthDate = value;
            }
        }

        public GooglePreferences GooglePreferences
        {
            get
            {
                return _googlePreferences;
            }
            set
            {
                if (_googlePreferences != value)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("GooglePreferences"));
                    }
                }
                _googlePreferences = value;
            }
        }

        #endregion

        #region Methods

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("FirstName", FirstName);
            info.AddValue("LastName", LastName);
            info.AddValue("BirthDate", BirthDate);
            info.AddValue("GooglePreferences", GooglePreferences);
        }

        public override string ToString()
        {
            return string.Format("{0}, {1}", LastName, FirstName);
        }

        #endregion
    }
}